package com.study.zouchao.superproject.mypublishtask.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.study.zouchao.superproject.R;
import com.study.zouchao.superproject.base_zou.ZouImgLoader;
import com.study.zouchao.superproject.comtaskdetail.model.TaskDetailResult;
import com.study.zouchao.superproject.comtasklist.fragment.model.entity.TaskListResult;
import com.study.zouchao.superproject.mypublishtask.model.entity.MyPublishTaskListResult;
import com.study.zouchao.superproject.util.LogLongUtil;
import com.study.zouchao.superproject.util.StringUtils;

import java.util.List;

import static com.baidu.location.d.j.v;

/**
 * Created by Administrator on 2017/6/20.
 */

public class MyPublishListAdapter extends RecyclerView.Adapter<MyPublishListAdapter.MyViewHolder> {
    private List<MyPublishTaskListResult.TaskItemBean> mDatas;
    private Context mContext;
    private IRvItemClickListener mListener;
    private View mFooterView;
    private static final int TYPE_FOOTER = 1;  //说明是带有Footer的
    private static final int TYPE_NORMAL = 2;  //说明是不带有header和footer的

    public MyPublishListAdapter(Context context, List<MyPublishTaskListResult.TaskItemBean> datas) {
        mContext = context;
        mDatas = datas;
    }

    public void setOnItemClickListener(IRvItemClickListener listener) {
        mListener = listener;
    }

    @Override
    public MyPublishListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mFooterView != null && viewType == TYPE_FOOTER) {
            return new MyPublishListAdapter.MyViewHolder(mFooterView);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_publish_task, parent, false);
        return new MyPublishListAdapter.MyViewHolder(view);
    }

    public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(getItemCount() - 1);
    }

    @Override
    public void onBindViewHolder(MyPublishListAdapter.MyViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_NORMAL) {
            if (holder != null) {
                //这里加载数据的时候要注意，是从position-1开始，因为position==0已经被header占用了
                MyPublishTaskListResult.TaskItemBean item = mDatas.get(position);
                LogLongUtil.logD("发起的任务列表 ", item.toString());

                holder.tvTitle.setText(StringUtils.isEmpty(item.getTaskTitle()) ? "暂无标题" : item.getTaskTitle());
                holder.tvStatus.setText(getNowStatus(item.getTaskStatus(), item.getEnrolledList()));
                holder.tvStatus.setTextColor(
                        item.getTaskStatus() >= 2 ? mContext.getResources().getColor(R.color.colorTextViewCommon)
                                : mContext.getResources().getColor(R.color.colorTextViewCanSelect));
                holder.tvContent.setText(StringUtils.isEmpty(item.getHelpContent()) ? "暂无内容" : item.getHelpContent());
                holder.tvTime.setText(StringUtils.isEmpty(item.getReleaseDate()) ? "" : item.getReleaseDate());
            }
        }
    }

    private String getNowStatus(int intNowStatus, List<MyPublishTaskListResult.TaskItemBean.EnrolledListBean> enrolledList) {
        String status;
        switch (intNowStatus) {
            case 1:
                status = "报名中(" + getEnrolledCount(enrolledList) + "人)..";
                break;
            case 2:
                status = "执行中..";
                break;
            case 3:
                status = "任务过期";
                break;
            case 4:
                status = "任务已完成";
                break;
            default:
                status = "状态有误";
                break;
        }
        return status;
    }

    private String getEnrolledCount(List<MyPublishTaskListResult.TaskItemBean.EnrolledListBean> enrolledList) {
        if (enrolledList == null || enrolledList.size() == 0) return "0";
        else return enrolledList.size() + "";
    }


    //返回View中Item的个数，这个时候，总的个数应该是ListView中Item的个数加上HeaderView和FooterView
    @Override
    public int getItemCount() {
        if (mFooterView == null) return mDatas.size();
        else return mDatas.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            //最后一个,应该加载Footer
            return TYPE_FOOTER;
        }
        return TYPE_NORMAL;
    }

    /**
     * Holder
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        View mItemView;
        TextView tvTitle, tvContent, tvTime, tvStatus;

        public MyViewHolder(final View itemView) {
            super(itemView);
            mItemView = itemView;
            tvTitle = (TextView) itemView.findViewById(R.id.id_tv_title);
            tvContent = (TextView) itemView.findViewById(R.id.id_tv_content);
            tvTime = (TextView) itemView.findViewById(R.id.id_tv_time);
            tvStatus = (TextView) itemView.findViewById(R.id.id_tv_status);

            mItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        if (mDatas.size() - 1 < getAdapterPosition()) return;
                        if (getAdapterPosition() < 0) return;
                        MyPublishTaskListResult.TaskItemBean itemBean = mDatas.get(getAdapterPosition());
                        mListener.onItemClickShowList(mItemView, itemBean.getTaskId(), itemBean.getEnrolledList(), getAdapterPosition(), itemBean.getTaskStatus());
                    }
                }
            });
        }

    }

    public interface IRvItemClickListener {
        void onItemClick(View v, int taskId, String enrolledUserName);

        void onItemClickShowList(View v, int taskId, List<MyPublishTaskListResult.TaskItemBean.EnrolledListBean> enrolledList, int position, int taskStatus);

    }
}
